﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MSIACCSL2.Web.PurchaseInvoiceServices;
using WcfService1.DTO;

namespace MSIACCSL2.Web.Posting
{
    public partial class PostingPurchaseInvoice : System.Web.UI.Page
    {
        private PurchaseInvoiceServicesClient piClient;
        public PostingPurchaseInvoice()
        {
            piClient = new PurchaseInvoiceServicesClient();
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
           ShowData();
        }

        void ShowData()
        {
            if (Page.IsValid)
            {
                var piList = piClient.GetListByPeriode(DateTime.Parse(txtTgl1.Text, new CultureInfo("ID-id")),
                                                         DateTime.Parse(txtTgl2.Text, new CultureInfo("ID-id")));
                GridView1.DataSource = piList.OrderBy(x => x.PINo);
                GridView1.DataBind();    
            }
            
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            //foreach (GridViewRow row in GridView1.Rows)
            //{
            //    Label lblStatus = (Label)row.FindControl("lblStatus");
            //    if (lblStatus.Text == "0")
            //        lblStatus.Text = "Open";
            //    else if (lblStatus.Text == "1")
            //        lblStatus.Text = "Close";
            //    else if (lblStatus.Text == "2")
            //        lblStatus.Text = "Void";
            //    else if (lblStatus.Text == "3")
            //        lblStatus.Text = "Post";
            //}
        }

        protected void btnCekAll_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chk1 = (CheckBox) row.FindControl("chk1");
                chk1.Checked = true;
            }
        }

        protected void btnPosting_Click(object sender, EventArgs e)
        {
            var pis = new List<PurchaseInvoiceDTO>();
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox check = (CheckBox) row.FindControl("chk1");
                    if (check.Checked)
                    {
                        pis.Add(new PurchaseInvoiceDTO(){PINo = row.Cells[0].Text});
                    }
                }
            }

            piClient.Posting(pis.ToArray());
            ShowData();
            Literal1.Text = "Proses Posting Selesai";
        }

        protected void btnUnPosting_Click(object sender, EventArgs e)
        {
            var pis = new List<PurchaseInvoiceDTO>();
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox check = (CheckBox)row.FindControl("chk1");
                    if (check.Checked)
                    {
                        pis.Add(new PurchaseInvoiceDTO() { PINo = row.Cells[0].Text });
                    }
                }
            }

            piClient.UnPosting(pis.ToArray());
            ShowData();
            Literal1.Text = "Proses UnPosting Selesai";
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            DateTime result;
            args.IsValid = DateTime.TryParse(txtTgl1.Text, out result) && DateTime.TryParse(txtTgl2.Text, out result);
        }
    }
}